from tkinter import *
import tkinter.messagebox
import threading
from bit_api import *
from DrissionPage import Chromium
from openpyxl import load_workbook
import afa

doc = 'D://谷歌开发者验证号(1).xlsx'
wb = load_workbook(doc, data_only=True)
pjsheet = wb[wb.sheetnames[0]]
class BrowItem:


    def __init__(self):
        global i
        self.id = int(round(time.time() * 1000))
        self.i = i
        self.L1 = Label(top, text="序号")
        self.L1.grid(row=i, column=0)

        self.E1 = Entry(top, bd=5)
        self.E1.grid(row=i, column=1)

        self.B = tkinter.Button(top, text="启动浏览器", command=lambda:self.openB())
        self.B.grid(row=i, column=2)

        self.B2 = tkinter.Button(top, text="登录", command=lambda: self.login())
        self.B2.grid(row=i, column=3)

        # self.B3 = tkinter.Button(top, text="点击验证", command=lambda: Open(pay_option, self))
        # self.B3.grid(row=i, column=4)

        self.B3 = tkinter.Button(top, text="填写密码", command=lambda: self.password())
        self.B3.grid(row=i, column=5)

        self.B4 = tkinter.Button(top, text="填写两步", command=lambda: Open(self.fa(), self))
        self.B4.grid(row=i, column=6)

        self.B5 = tkinter.Button(top, text="开发者", command=lambda: self.developer())
        self.B5.grid(row=i, column=7)

        # self.B6 = tkinter.Button(top, text="更换IP", command=lambda: changeIP(self))
        # self.B6.grid(row=i, column=8)

        i += 1

    def get_entry(self):
        return self.E1.get()

    def get_id(self):
        return self.id

    def set_id(self):
        self.id = int(round(time.time() * 1000))

    def openbt(self):
        get_ = pjsheet[f'K{self.E1.get()}'].value
        res = openBrowser(get_)
        driverPath = res['data']['driver']
        debuggerAddress = res['data']['http']
        print(driverPath,debuggerAddress)
        self.debuggerAddress = debuggerAddress
        self.login()

    def openB(self):
        t1 = threading.Thread(target=self.openbt)
        t1.start()


    def login(self):
        browser = Chromium(self.debuggerAddress)
        self.tab = browser.new_tab()
        self.tab.get('http://accounts.google.com')
        username = self.tab.ele('@id=identifierId')
        username.clear()
        username.input(pjsheet[f'F{self.E1.get()}'].value)
        nextstep = self.tab.ele('@class=TNTaPb')
        nextstep.click()
        # nextstep = tab.ele('@title=reCAPTCHA')
        # nextstep.click()



    def f(self):
        password = self.tab.ele('@type=password')
        password.clear()
        password.input(pjsheet[f'G{self.E1.get()}'].value)
        nextstep = self.tab.ele('@class=TNTaPb')
        nextstep.click()
        time.sleep(5)
        self.fa()



    def password(self):
        t1 = threading.Thread(target=self.f)
        t1.start()



        pass

    def fa(self):
        totp = afa.compute_totp(pjsheet[f'L{self.E1.get()}'].value)
        print(totp)
        fa2ele = self.tab.ele('@type=tel')
        fa2ele.input(totp)
        nextstep = self.tab.ele('@class=TNTaPb')
        nextstep.click()
        pass

    def developer(self):
        browser = Chromium(self.debuggerAddress)
        tab = browser.new_tab()
        tab.get('https://play.google.com/console')
        nextstep = tab.ele('@debug-id=all-developers')
        nextstep.click()




class myThread(threading.Thread):
    def __init__(self, threadID, name, counter, option, entry, id1):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.option = option
        self.entry = entry
        self.id = id1


    def run(self):
        pass
        # os.system(startBrowserCommand + ' ' + self.option + ' ' + self.entry + ' ' + str(self.id))


def Open(option, brow_item):
    global threadId
    global start_option
    if option == start_option:
        brow_item.set_id()
    threadId += 1
    print(brow_item.get_id())
    thread1 = myThread(threadId, "Thread-" + str(threadId), threadId, option, brow_item.get_entry(), brow_item.get_id())
    thread1.start()


def Gmail(entry):
    global threadId
    threadId += 1
    thread1 = myThread(threadId, "Thread-" + str(threadId), threadId, entry)
    thread1.start()


if __name__ == '__main__':
    top = Tk()
    # hwnd = pywintypes.HANDLE(int(top.frame(), 16))
    # print(top.frame())
    # system = os.system('H:\\911\\3.35\\ProxyTool\\ProxyAPI -changeproxy/US -zip=92626 -proxyport=5000 -hwnd=' + str(top.winfo_id()))
    # print(system)
    for i in range(0, 5):
        BrowItem()
    # top.overrideredirect(True)
    # top.attributes("-alpha", 1)
    top.wm_attributes('-topmost', 1)
    top.mainloop()
